package com.example.tilitili.controller;

import com.example.tilitili.pojo.User;
import com.example.tilitili.service.IOrderService;
import com.example.tilitili.vo.GoodsVo;
import com.example.tilitili.vo.OrderDetailVo;
import com.example.tilitili.vo.RespBean;
import com.example.tilitili.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liu
 * @since 2022-01-17
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    /**
     * 订单详情
    * @param user
    * @param orderId
    * @return
    */
    @RequestMapping("/detail")
    @ResponseBody
    public RespBean detail(User user, Long orderId){
        if (null==user){
            return RespBean.error(RespBeanEnum.SESSION_ERROR);
        }
        OrderDetailVo detail = orderService.detail(orderId);
        return RespBean.success(detail);
    }
    /**
     * 全部订单
     */
    @RequestMapping("/orderall")
    public String toLogin2( Model model, User user) {
        model.addAttribute("user", user);
        model.addAttribute("orderList", orderService.findOrderVo());
        return "orderList";
    }
    /**
     * 跳转订单详情页
     */
    @RequestMapping(value = "/detail/{orderId}",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail2(HttpServletRequest request, HttpServletResponse
            response, Model model, User user, @PathVariable Long orderId) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //Redis中获取页面，如果不为空，直接返回页面
        String html = (String) valueOperations.get("orderDetail:" + orderId);
        if (!StringUtils.isEmpty(html)) {
            return html;
        }
        model.addAttribute("user", user);
        OrderDetailVo order=orderService.findOrderDetailVoByOrderId(orderId);
        model.addAttribute("order", order);
        // return "goodsDetail";
        //如果为空，手动渲染，存入Redis并返回
        WebContext context = new WebContext(request, response,
                request.getServletContext(), request.getLocale(),
                model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("orderDetail",
                context);
        if (!StringUtils.isEmpty(html)) {
            valueOperations.set("orderDetail:" + orderId, html, 60,
                    TimeUnit.SECONDS);
        }
        return html;
    }
}
